(function($) {
	$.getUrlParam = function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null)
			return unescape(r[2]);
		return null;
	}
})(jQuery);
$(function() {

	currentPageArray = new Array();
	var key = decodeURI(escape($.getUrlParam('key')));
	var kind = decodeURI(escape($.getUrlParam('search_kind')));
	// 这里后面可以改
	var size = 15;
	var tempgoods = null;
	var allpages = null;
	var isStart = true;
	var ischange = false;
	var initsize = 600;
	var flag = true;
	var now = 0;
	currentPageArray = new Array();

	$.ajax({
		url : base_path + "/searchresult.do",
		type : "get",
		data : {
			"page" : 1,
			"size" : size,
			"key" : key,
			"kind" : kind
		},
		dataType : "json",
		success : function(result) {
			tempgoods = result;
			updateList(result);
			allpages = result[0].allPages;
			InitPaging(allpages);
			now = 1;
		},
		error : function() {
			alert("服务器繁忙");
		}
	});

	$("#page ")
			.on(
					"click",
					"li .ye",
					function() {
						var num = parseInt(this.text);
						var num2 =  2*parseInt(this.text)-1;
						$.ajax({
							url : base_path + "/searchresult.do",
							type : "get",
							data : {
								"page" : num2,
								"size" : size,
								"key" : key,
								"kind" : kind
							},
							dataType : "json",
							success : function(result) {
								tempgoods = result;
								updateList(result);
								now = num2;
								flag=true;
							},
							error : function() {
								alert("服务器繁忙");
							}
						});
						
						var index = currentPageArray.indexOf(num);
						console.log(index);
						// 如果是数组的第一位 但不是pageSize的第一位 那么原来的数组进行变动
						if (index == 0 && num != 1) {
							for (var i = 0; i < currentPageArray.length; i++) {
								currentPageArray[i] = currentPageArray[i] - 1;
							}
							console.log("向前变" + currentPageArray);
							ischange = true;
						}
						// 向后变
						if (index == 9 && num != pageSize) {
							for (var i = 0; i < currentPageArray.length; i++) {
								currentPageArray[i] = currentPageArray[i] + 1;
							}
							console.log("向后变" + currentPageArray);
							ischange = true;
						}
						if (ischange == true) {
							ischange = false;
							$("#page").empty();
							$("#page")
									.append(
											"<li><a id=\"head\" href=\"javascript:void(0)\">首页</a></li");
							for (i = 0; i < currentPageArray.length; i++) {
								$("#page").append(
										"<li><a class=\"ye\" href=\"javascript:void(0)\">"
												+ currentPageArray[i]
												+ "</a></li");
							}
							$("#page")
									.append(
											"<li><a id=\"tail\" href=\"javascript:void(0)\">尾页</a></li");
						}

					});

	$("#page").on("click", "#head", function() {
		$.ajax({
			url : base_path + "/searchresult.do",
			type : "get",
			data : {
				"page" : 1,
				"size" : size,
				"key" : key,
				"kind" : kind
			},
			dataType : "json",
			success : function(result) {
				tempgoods = result;
				updateList(result);
				showStartPaging();
				now = 1;
				flag=true;
			},
			error : function() {
				alert("服务器繁忙");
			}
		});
	});

	$("#page").on("click", "#tail", function() {
		$.ajax({
			url : base_path + "/searchresult.do",
			type : "get",
			data : {
				"page" : allpages,
				"size" : size,
				"key" : key,
				"kind" : kind
			},
			dataType : "json",
			success : function(result) {
				tempgoods = result;
				updateList(result);
				showEndPaging();
				now = allpages;
				flag=true;
			},
			error : function() {
				alert("服务器繁忙");
			}
		});
	});

	$(".complex").toggle(function() {
		var type = $(this).find(".compare").text();
		var way = "asc";
		var content = JSON.stringify(tempgoods);
		$.ajax({
			url : base_path + "/sort.do",
			type : "post",
			data : {
				"type" : type,
				"way" : way,
				"content" : content
			},
			dataType : "json",
			success : function(result) {
				updateList(result);
			},
			error : function() {
				alert("服务器繁忙");
			}
		});
	}, function() {
		var type = $(this).find(".compare").text();
		var way = "desc";
		var content = JSON.stringify(tempgoods);
		$.ajax({
			url : base_path + "/sort.do",
			type : "post",
			data : {
				"type" : type,
				"way" : way,
				"content" : content
			},
			dataType : "json",
			success : function(result) {
				updateList(result);
			},
			error : function() {
				alert("服务器繁忙");
			}
		});
	});
	$(window).scroll(function ()
			{
				var st = parseInt($(this).scrollTop());
				if(st>initsize && flag){
		            flag=false;
		            
		    		$.ajax({
						url : base_path + "/searchresult.do",
						type : "get",
						data : {
							"page" : now+1,
							"size" : size,
							"key" : key,
							"kind" : kind
						},
						dataType : "json",
						success : function(result) {
							addList(result);
							tempgoods.push(result[0]);
							//json对象合并
							//tempgoods=JSON.parse(tempgoods).concat(result);
//							flag=true;
						},
						error : function() {
							alert("服务器繁忙");
						}
					});
		        }
			});
});

function InitPaging(pageSize) {
	if (pageSize <= 10) {
		currentPageArray.length = pageSize;
	} else {
		currentPageArray.length = 10;
	}
	if(pageSize!=0){
		$("#page").append(
		"<li><a id=\"head\" href=\"javascript:void(0)\">首页</a></li");	
	}

	for (var i = 0; i < currentPageArray.length; i++) {
		currentPageArray[i] = i + 1;
		$("#page").append(
				"<li><a class=\"ye\" href=\"javascript:void(0)\">" + (i + 1)
						+ "</a></li");
	}
	if(pageSize!=0){
	$("#page").append(
			"<li><a id=\"tail\" href=\"javascript:void(0)\">尾页</a></li");
	}

}
function updateList(result) {
	$(".view").empty();
	pageSize = result[0].allPages;
	var list = result[0].result;
	var sli = "";
	var item;
	for ( var i in list) {
		item = list[i];
		sli += "<div class=\"product\">";
		sli += "<div class=\"product-wrap\">";
		sli += "<div class=\"productImg\">";
		sli += "<a href=\"item.do?productId=" + item.productId
				+ "\" class=\"productImg-wrap\"> <img src=\"uploads/"
				+ item.productImgAdd + "\" alt=\"\"></a>";
		sli += "</div>";
		sli += "<p class=\"productPrice\">";
		sli += "<em title=\"78.5\"> <b>¥</b>" + item.price + "</em>";
		sli += "</p>";
		sli += "<p class=\"productTitle\">";
		sli += "<a href=\"\">" + item.productName + "</a>";
		sli += "</p>";
		sli += "<div class=\"productShop\">";
		sli += "<a class=\"productShop-name\" href=\"\">" + item.seller.salerName + "</a>";
		sli += "</div>";
		sli += "<p class=\"productStatus\">";
		sli += "<span class=\"amount\">销量 <em>" + item.saleAmount + "笔</em>";
		sli += "</span> <span class=\"wangwang\"> <a href=\"\"> <img src=\"img/icon_wangwang.png\" alt=\"\">";
		sli += "</a>";
		sli += "</span>"
		sli += "</p>";
		sli += "</div>";
		sli += "</div>";
	}
	$(".view").append(sli);
	document.body.scrollTop=0;
}

function showStartPaging() {
	$("#page").empty();
	$("#page").append(
			"<li><a id=\"head\" href=\"javascript:void(0)\">首页</a></li");
	for (var i = 0; i < currentPageArray.length; i++) {
		$("#page").append(
				"<li><a class=\"ye\" href=\"javascript:void(0)\">" + (i + 1)
						+ "</a></li");
		currentPageArray[i]= i + 1;
	}
	$("#page").append(
			"<li><a id=\"tail\" href=\"javascript:void(0)\">尾页</a></li");
}

function showEndPaging() {
	$("#page").empty();
	$("#page").append(
			"<li><a id=\"head\" href=\"javascript:void(0)\">首页</a></li");
	var endlength = pageSize % 10;
	
	
	if (endlength != 0) {
		//长度小于10 比如是endlength = 7 那么你还要加3个进来
		for(var z=0;z<10-endlength;z++){
			$("#page").append(
					"<li><a class=\"ye\" href=\"javascript:void(0)\">"
							+ (pageSize - endlength + 1-10+endlength+z) + "</a></li");
			currentPageArray[z]= pageSize - endlength + 1-10+endlength+z;
		}
		
		
		for (var i = 0; i < endlength; i++) {
			$("#page").append(
					"<li><a class=\"ye\" href=\"javascript:void(0)\">"
							+ (pageSize - endlength + i + 1) + "</a></li");
			currentPageArray[i+10-endlength]= pageSize - endlength + i + 1;
		}
		
		
	} else {
		for (var i = pageSize - 10; i < pageSize; i++) {
			$("#page").append(
					"<li><a class=\"ye\" href=\"javascript:void(0)\">"
							+ (i + 1) + "</a></li");
			currentPageArray[i]=  i + 1;
		}
	}
	$("#page").append(
			"<li><a id=\"tail\" href=\"javascript:void(0)\">尾页</a></li");
}


function addList(result) {
	pageSize = result[0].allPages;
	var list = result[0].result;
	var sli = "";
	var item;
	for ( var i in list) {
		item = list[i];
		sli += "<div class=\"product\">";
		sli += "<div class=\"product-wrap\">";
		sli += "<div class=\"productImg\">";
		sli += "<a href=\"item.do?productId="+item.productId+"\" class=\"productImg-wrap\"> <img src=\"uploads/"+item.productImgAdd+"\" alt=\"\"></a>";
		sli += "</div>";
		sli += "<p class=\"productPrice\">";
		sli += "<em title=\"78.5\"> <b>¥</b>"+item.price+"</em>";
		sli += "</p>";
		sli += "<p class=\"productTitle\">";
		sli += "<a href=\"\">"+item.productName+"</a>";
		sli += "</p>";
		sli += "<div class=\"productShop\">";
		sli += "<a class=\"productShop-name\" href=\"\">"+item.saler+"</a>";
		sli += "</div>";
		sli += "<p class=\"productStatus\">";
		sli += "<span class=\"amount\">销量 <em>"+item.saleAmount+"笔</em>";
		sli += "</span> <span class=\"wangwang\"> <a href=\"\"> <img src=\"img/icon_wangwang.png\" alt=\"\">";
		sli += "</a>";
		sli += "</span>"
		sli += "</p>";
		sli += "</div>";
		sli += "</div>";
	}
	$(".view").append(sli);
}

